﻿//-----------------------------------------------------------------------
// <copyright file="TickersViewModel.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Windows.Tickers
{
    using System.Windows;
    using System.Windows.Threading;

    using ChatBox.DomainObjects.ResultData;
    using ChatBox.DomainObjects.Settings;
    using ChatBox.Interfaces;
    using ChatBox.UI.ViewModels;
    using ChatBox.Utility;
    using ChatBox.XamlUtility;

    /// <summary>
    /// view model for the tickers window
    /// </summary>
    public class TickersViewModel : WindowViewModel
    {
        #region data

        /// <summary>
        /// The source of Ui settings
        /// </summary>
        private readonly IUISettingsRepository uiSettingsRepository;

        /// <summary>
        /// View model for random nodes data
        /// </summary>
        private readonly RandomNodesViewModel randomNodes = new RandomNodesViewModel();

        /// <summary>
        /// View model for cool nodes data
        /// </summary>
        private readonly CoolNodesViewModel coolNodes = new CoolNodesViewModel();

        /// <summary>
        /// View model for editor cools data
        /// </summary>
        private readonly EditorCoolsViewModel editorCools = new EditorCoolsViewModel();

        /// <summary>
        /// View model for new writeups data
        /// </summary>
        private readonly NewWriteupsViewModel newWriteups = new NewWriteupsViewModel();

        /// <summary>
        /// The chat engine
        /// </summary>
        private readonly IChatEngine chatEngine;

        #endregion

        /// <summary>
        /// Initializes a new instance of the TickersViewModel class
        /// </summary>
        /// <param name="chatEngine">the chat engine</param>
        /// <param name="uiSettingsRepository">the source of ui settings</param>
        public TickersViewModel(IChatEngine chatEngine, IUISettingsRepository uiSettingsRepository)
        {
            this.uiSettingsRepository = uiSettingsRepository;

            this.chatEngine = chatEngine;
            this.chatEngine.OperationCompleted += this.ChatEngineOperationCompleted;
            this.chatEngine.StartTickers();
        }

        /// <summary>
        /// Gets or sets the hyperlink context
        /// </summary>
        public HyperlinkContext HyperlinkContext
        {
            get
            {
                // retrieve from any
                return this.RandomNodes.HyperlinkContext;
            }

            set
            {
                // use the same hyperlink contetx on all of them
                this.NewWriteups.HyperlinkContext = value;
                this.EditorCools.HyperlinkContext = value;
                this.CoolNodes.HyperlinkContext = value;
                this.RandomNodes.HyperlinkContext = value;
            }
        }

        /// <summary>
        /// Gets the chat engine
        /// </summary>
        public IChatEngine ChatEngine
        {
            get { return this.chatEngine; }
        }

        /// <summary>
        /// Gets the cool nodes view model 
        /// </summary>
        public CoolNodesViewModel CoolNodes
        {
            get
            {
                return this.coolNodes;
            }
        }

        /// <summary>
        /// Gets the random nodes view model 
        /// </summary>
        public EditorCoolsViewModel EditorCools
        {
            get
            {
                return this.editorCools;
            }
        }

        /// <summary>
        /// Gets the random nodes view model 
        /// </summary>
        public NewWriteupsViewModel NewWriteups
        {
            get
            {
                return this.newWriteups;
            }
        }

        /// <summary>
        /// Gets the random nodes view model 
        /// </summary>
        public RandomNodesViewModel RandomNodes
        {
            get
            {
                return this.randomNodes;
            }
        }

        #region methods

        /// <summary>
        /// Save the window size and position
        /// </summary>
        public void SaveWindowPosition()
        {
            UISettingsData settingsData = this.uiSettingsRepository.ReadSettings();
            settingsData.TickersWindowDimensions = new Rect(this.Window.Left, this.Window.Top, this.Window.Width, this.Window.Height);

            this.uiSettingsRepository.SaveSettings(settingsData);
        }

        /// <summary>
        /// Load the window size and position
        /// </summary>
        public void LoadWindowPosition()
        {
            UISettingsData settingsData = this.uiSettingsRepository.ReadSettings();
            this.Window.SetWindowPosition(settingsData.TickersWindowDimensions);
        }

        /// <summary>
        /// Data received from the chat engine
        /// </summary>
        /// <param name="sender">the event sender</param>
        /// <param name="e">the event params</param>
        private void ChatEngineOperationCompleted(object sender, EventArgs<OperationResult> e)
        {
            // get onto the right thread
            Application.Current.Dispatcher.Invoke(
                    DispatcherPriority.Normal,
                    new OperationResultHandler(this.ProcessOperationResult),
                    e.Value);
        }

        /// <summary>
        /// Data received from the chat engine, on the right thread now
        /// </summary>
        /// <param name="operationResult">the data from the engine</param>
        private void ProcessOperationResult(OperationResult operationResult)
        {
            // one of these should know what to do with the data 
            this.NewWriteups.ProcessOperationResult(operationResult);
            this.CoolNodes.ProcessOperationResult(operationResult);
            this.EditorCools.ProcessOperationResult(operationResult);
            this.RandomNodes.ProcessOperationResult(operationResult);
        }
        
        #endregion
    }
}
